﻿$(function () {

	var names = [
		'武杰', '申坚', '王玮琪', '刘声隆', '郭志贤', '左赛军',
		'谭婷', '尹海峰', '曾根', '袁秒鑫'
	];

	var $name = $('span.name'),
		$list = $('div.list'),
		$count = $('#count');
	var count = 0,
		speed = 5,
		lastTime = 5,
		countDownStopForMS = 10000,
		countDownStopForS = countDownStopForMS / 1000;

	$('#btn').click(function () {
		var $this = $(this);
		if ($this.is('.start')) {
			var nameTimerNo = setInterval(function () {
				var index = Math.floor(Math.random() * names.length);
				var name = names[index];
				$name.text(name);
			}, speed);
			$this.removeClass('start').addClass('stop').text(countDownStopForS + '秒后停止');

			var curStep = 0, lowSpeed = 100;
			var countDownStopTimerNo = setInterval(function () {
				curStep ++;
				$this.text(countDownStopForS - curStep + '秒后停止');
				if (countDownStopForS - curStep <= lastTime) {
					clearInterval(nameTimerNo);
					nameTimerNo = setInterval(function () {
						var index = Math.floor(Math.random() * names.length);
						var name = names[index];
						$name.text(name);
					}, lowSpeed += 100);
				}
				if (curStep === countDownStopForS) {
					clearInterval(countDownStopTimerNo);
					clearInterval(nameTimerNo);
					var name = $name.text();
					$list.append($('<span>' + name + '</span>'));
					count ++;
					$count.text(count);
					$this.removeClass('stop').addClass('start').text('开始');
				}
			}, 1000);
		}
	});
});